home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / roids / shot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.3 KB  |  128 lines

  1. /*
  2.  * Copyright 1989 Digital Equipment Corporation
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Digital Equipment
  9.  * Corporation not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  Digital Equipment Corporation makes no representations
  12.  * about the suitability of this software for any purpose.  It is
  13.  * provided "as is" without express or implied warranty.
  14.  *
  15.  * DIGITAL EQUIPMENT CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
  18.  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Terry Weissman
  24.  *          weissman@decwrl.dec.com
  25.  */
  26.  
  27. /* shot.c - handle movement, etc. of the shots. */
  28.  
  29. #include "roids.h"
  30.  
  31. #define MAXSHOTS    20
  32.  
  33. typedef struct _ShotRec {
  34.     double x, y;        /* Location of this shot. */
  35.     double vx, vy;        /* Velocity of this shot. */
  36.     int count;            /* Countdown of life remaining to this shot. */
  37. } ShotRec, *Shot;
  38.  
  39. ShotRec shots[MAXSHOTS];
  40.  
  41. static int numshots;        /* Number of shots currently flying. */
  42.  
  43. static int shotcount;        /* Initial value for life countdown. */
  44.  
  45. static void PaintShot(shot, gc)
  46. Shot shot;
  47. GC gc;
  48. {
  49.     AddRec(rint(shot->x), rint(shot->y), gc);
  50. }
  51.  
  52.  
  53. void PaintAllShots()
  54. {
  55.     int i;
  56.     for (i=0 ; i<numshots ; i++)
  57.     PaintShot(shots + i, shotgc);
  58. }
  59.  
  60.  
  61. static void DestroyShot(i)
  62. int i;
  63. {
  64.     PaintShot(shots + i, backgc);
  65.     numshots--;
  66.     shots[i] = shots[numshots];
  67. }
  68.  
  69.  
  70. void MoveShots(closure, id)
  71. Opaque closure;
  72. XtIntervalId id;
  73. {
  74.     int i;
  75.     double newx, newy;
  76.     Shot shot;
  77.     if (closure != (Opaque) MoveShots) return;
  78.     if (numshots > 0)
  79.         shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
  80.     else
  81.     shottimerid = NULL;
  82.     for (i=0 ; i<numshots ; i++) {
  83.     shot = shots + i;
  84.     newx = shot->x + shot->vx;
  85.     newy = shot->y + shot->vy;
  86.     if (LineHitsRock(rint(shot->x - shot->vx), rint(shot->y - shot->vy),
  87.              rint(newx), rint(newy))
  88.         || --(shot->count) == 0) {
  89.         DestroyShot(i);
  90.         i--;        /* Ensures we don't skip moving a shot. */
  91.     } else {
  92.         PaintShot(shot, backgc);
  93.         shot->x = newx < 0 ? newx + gamewidth :
  94.         (newx > gamewidth ? newx - gamewidth : newx);
  95.         shot->y = newy < 0 ? newy + gameheight :
  96.         (newy > gameheight ? newy - gameheight : newy);
  97.         PaintShot(shot, shotgc);
  98.     }
  99.     }
  100. }
  101.  
  102.  
  103.  
  104. void AddShot(x, y, vx, vy)
  105. double x, y, vx, vy;
  106. {
  107.     Shot shot;
  108.     if (numshots >= maxshots) return;
  109.     shot = shots + numshots;
  110.     numshots++;
  111.     shot->x = x;
  112.     shot->y = y;
  113.     shot->vx = vx;
  114.     shot->vy = vy;
  115.     shot->count = shotcount;
  116.     if (shottimerid == NULL)
  117.         shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
  118. }
  119.  
  120.  
  121.  
  122. InitShot()
  123. {
  124.     shottimerid = NULL;
  125.     shotcount = shotduration / shotwait;
  126.     numshots = 0;
  127. }
  128.